<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";



class SearchPage extends IPlugin 
{
    var $pagespool;

    function SearchPage($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->pagespool = null;
        $this->initialize();
    }
    
    function getPluginTag() { return "searchpage"; }
    function getPluginName() { return "Search Page plugin"; }
    function getPluginVersion() { return "0.0.1"; }

    function getProperty($p = null)
    {
        $result = "";
        return $result;
    }
    
    function initialize()
    {
        $this->pagespool = array();
        if ($handle = opendir($this->tplfolder))
        {
            while (false !== ($file = readdir($handle)))
            {
                if("."==$file || ".."==$file || !file_exists($this->tplfolder."/".$file))
                {
                    continue;
                }
                $fl = split("\.", $file);
                $this->pagespool[] = $fl[0];
            }
            closedir($handle);
        }
        $this->pagespool = array_flip($this->pagespool);
        $core = &getCoreInstance();
        $core->setValues("searchpage_url", "?".$this->getURL()."&page=");
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------

    function doAction($params){
    }
   
    function Render($params){
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        $page_name = 'search.tpl.php';
        $core = &getCoreInstance();
        $render = &$core->framework->smarty;
        $db = &$core->getProperty("db");

        //--------------------------------------------------------
        //   Search
        //--------------------------------------------------------
        
        $param=0;
 
                $search_n="";
        if (isset($_POST[search_n])) $search=$_POST[search_n];
        if (isset($_GET[search_n])) $search=$_GET[search_n];
                if (isset($_POST[type])) $type=$_POST[type];
        if (isset($_GET[type])) $type=$_GET[type];
                
                $search_n=$search;
        if ($search!="" && $search!="Any Product")
        {
            $search_where.=" and (lower(title) like '%".strtolower(str2db($search))."%' or lower(short_desc) like '%".strtolower(str2db($search))."%'  or lower(description) like '%".strtolower(str2db($search))."%' or `our_price` like '%".strtolower(str2db($search))."%')";
            if ($param) $result.=","; 
            
            $search = str_replace("<","&lt;",$search);
            $search = str_replace(">","&gt;",$search);
                        $result.=" for product like: \"<b>$search</b>\"";
            $param=1;
            $search=0;
        }
//              $search_where.=" and type='$type'";
                //debug($search_where); 
        
   // --------------------------------- result!
        if (!$param) $result=" <font color=\"red\"> \"Any\"</font> product in \"$type\" !";
        $count=$db->getone("select count(*) from catalog where is_category= 'N' and is_active= 'Y' $search_where");
        //debug("select count(*) from catalog where iscategory= 'N' and isactive= 'Y' $search_where");
        $result = " Search ".$result." &rsaquo;&rsaquo; FOUND <b>$count</b> ITEMS";
        $render->assign("result", $result);     
        
        $sql = "select catalog_id,parent_id,item_number,title,is_active,img_small,our_price as price from catalog where is_category= 'N' and is_active = 'Y' $search_where order by catalog_id";
        #debug($sql , "sql");
        $products = $db->getRow($sql);
                
        //-----------------     paging[
        
        $p = $core->getSnippet("paging");
        $page = 0;
        $ppg = 0;
        $params = array(
            "source" => "sql",            
            "sql_count" => "select count(*) from catalog where is_active = 'Y' and is_category = 'N' $search_where",
            "ippg" => 9,
            "nppv" => 5,
            "urlbase" => "?path=searchpage&search_n=$search_n",
            "page" => &$page,
            "ppg" => &$ppg,
            "noseolinks" => true
            );
        $render->assign("paging", $p->Render($params));
        //-----------------     paging ]
        $list_products = ArrayDb2Str($db->getAll("select catalog_id,parent_id,item_number,title,is_active,img_small,our_price 
                                                                                                        from catalog 
                                                                                                        where is_active = 'Y' and is_category='N' $search_where order by catalog_id",
                                                                                0, false, $page==-1 ? 100000 : 9, $page==-1 ? 0 : $page*9));
                // ----------- check img + price format[
                for ($i=0;$i<count($list_products);$i++)
                {
                        if (file_exists($core->config["file_root"]."/inventory/".$list_products[$i][img_small]) && is_file($core->config["file_root"]."/inventory/".$list_products[$i][img_small])) {
                                list($w_size,$h_size)=explode(',',$this->getimgsizes($core->config["file_root"],$list_products[$i][img_small]));
                                $default_w_size=118;
                                $default_h_size=122;
                                $size="";
                                if ($w_size>$h_size && $w_size>$default_w_size)
                                        $size=" width=\"{$default_w_size}px\" ";
                                if ($h_size>$w_size && $h_size>$default_h_size)
                                $size=" height=\"{$default_h_size}px\" ";
                                
                                $list_products[$i][img_small] = "<img style='cursor:hand' alt='View ".$list_products[$i]['title']."' src=\"/inventory/".$list_products[$i][img_small]."\" border=\"0\" />";
                        } else {
                                $list_products[$i][img_small] = "<img style='cursor:hand' alt='' src='/img/no_image.jpg' border='0' />";
                        }

                $pid = $list_products[$i]["parent_id"];
                $path = array();
                while($pid!=0) {
                        $row = $db->getRow("select parent_id from catalog where catalog_id={$pid}");
                        $pid = $row["parent_id"];
                }
        
                $path = @join("/", array_reverse($path));
                $path = $path!="" ? $path."/" : "";

//                      $list_products[$i][seo] = "/catalog/".$path.$list_products[$i]["rname"]; //.".html";
                        $list_products[$i][our_price] = number_format($list_products[$i][our_price],2,'.',',');         
                }
                // ----------- check img + price format]
                $const_search_ipp = 3;
                if( !empty($core->config['system']["search_ipp"]) )
                    $const_search_ipp = $core->config['system']["search_ipp"];
                #debug($core->config['system']["search_ipp"] , "se");
                
                $render->assign("ipp" , $const_search_ipp);
                #search_ipp
                $render->assign("list_products", $list_products);
                
        return $render->fetch("file:".$this->tplfolder."/".$page_name);
    }    
        
// ----------------------------------------------------------------------------------
// --- [ GET IMAGES SIZE ] ----------------------------------------------------------
// ----------------------------------------------------------------------------------

        function getimgsizes($froot,$img){ 
                if(isne($img) && is_file($froot.'/inventory/'.$img)) {
                        $img = $froot.'/inventory/'.$img;
                        $ar = getimagesize($img);
                        $arrr = $ar[0].','.($ar[1]+50);
                        return $arrr;
                }
                return "";
        } 

    
};  

?>